package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.service.BusinessService;
import com.atguigu.service.MemberService;
import com.atguigu.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/report")
@RestController
public class ReportController {

    @Reference
    MemberService memberService;


    @Reference
    BusinessService businessService;

    @Reference
    SetmealService setmealService;

    @RequestMapping("/getMemberReport.do")
    public Result getMemberReport(){
        Map<String,List> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        List<Integer> CountByMonth = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String month = new String(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
            months.add(month);
        }
        CountByMonth = memberService.getMemberCountByMonths(months);
        map.put("memberCount",CountByMonth);
        map.put("months",months);
        return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getSetmealReport.do")
    public Result getSetmealReport(){
        try {
            Map<String,List> map = new HashMap<>();
            List<String> setmealNames = new ArrayList<>();

            List<Map<String,Object>> setmealCount = new ArrayList<>();
            setmealNames = setmealService.getSetmealname();
            setmealCount = setmealService.getSetmealCountByName(setmealNames);
            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/getBusinessReportData.do")
    public Result getBusinessReportData() throws Exception {
        Map map = businessService.getBusinessReportData();


        return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
    }
}











